﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SAP.WebServices;
using System.Collections;
using System.Globalization;
using System.Net;
using Microsoft.Reporting.WebForms;
using System.IO;

namespace SAP
{
    public partial class GeneralJournal : System.Web.UI.Page
    {
        public static string ReportID = "1";        
        public static string ReportFileName = "GeneralJournal.rdl";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        public void ViewReport(ReportViewer reportViewer, DataSet ds)
        {
            string strReport = System.IO.File.ReadAllText(Server.MapPath(ReportFileName), System.Text.Encoding.Default); 
            if (strReport.Contains("http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"))
            {
                strReport = strReport.Replace("<Report xmlns:rd=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\" xmlns:cl=\"http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition\" xmlns=\"http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition\">", "<Report xmlns:rd=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\" xmlns=\"http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition\">");
                strReport = strReport.Replace("<ReportSections>", "").Replace("<ReportSection>", "").Replace("</ReportSection>", "").Replace("</ReportSections>", "");
            }
            byte[] bytReport = System.Text.Encoding.Default.GetBytes(strReport);
            reportViewer.LocalReport.LoadReportDefinition(new MemoryStream(bytReport));

            object objReportDataSource = ds.Tables[0];
            reportViewer.Reset();
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource(ds.Namespace.ToString(), objReportDataSource));
            reportViewer.LocalReport.Refresh();
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            if (txtFromDate.Text == "" || txtToDate.Text == "")
                return;
            CultureInfo ivC = new System.Globalization.CultureInfo("es-US");
            Reports rpt = new Reports();
            DataSet ds = rpt.VAS_GeneralJournalReport(User.Identity.Name, Convert.ToDateTime(txtFromDate.Text, ivC), Convert.ToDateTime(txtToDate.Text, ivC));

            ViewReport(objReportViewer, ds);
        }
    }
}

/* dynamic loading reporting from database with xml
 * public static DataTable rptDataTable;
 * gf.RunQuery("Update ReportList Set ReportXML='"+strReport+"'");
 * rptDataTable.Rows[0]["ReportXML"].ToString();
 * GeneralFunctions gf = new GeneralFunctions(User.Identity.Name);
 * rptDataTable = gf.RunQuery("Select ReportXML from ReportList where reportID=" + ReportID.ToString());
 * this.Page.Title = rptDataTable.Rows[0]["ReportName"].ToString();
*/